<!doctype html>



  


<html class="theme-next pisces use-motion">
<head>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PBHRK7');</script>
<!-- End Google Tag Manager -->
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="前端 知识 面试," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.2" />






<meta name="description" content="目录  &amp;emsp;1. 说一下Vue的双向绑定数据的原理 &amp;emsp;2. 解释单向数据流和双向数据绑定 &amp;emsp;3. Vue如何去除url中的 # &amp;emsp;4. 对 MVC、MVVM的理解 &amp;emsp;5. 介绍虚拟DOM &amp;emsp;6. vue生命周期的理解 &amp;emsp;7. 组件通信 &amp;emsp;8. 路由实现 &amp;emsp;9. v-if 和 v-show 区别 &amp;emsp;10">
<meta name="keywords" content="前端 知识 面试">
<meta property="og:type" content="article">
<meta property="og:title" content="前端面试《第十五篇》 Vue.js">
<meta property="og:url" content="http://wurh.github.io/2019/03/31/201904010027/index.html">
<meta property="og:site_name" content="华Ing&#39;s blog">
<meta property="og:description" content="目录  &amp;emsp;1. 说一下Vue的双向绑定数据的原理 &amp;emsp;2. 解释单向数据流和双向数据绑定 &amp;emsp;3. Vue如何去除url中的 # &amp;emsp;4. 对 MVC、MVVM的理解 &amp;emsp;5. 介绍虚拟DOM &amp;emsp;6. vue生命周期的理解 &amp;emsp;7. 组件通信 &amp;emsp;8. 路由实现 &amp;emsp;9. v-if 和 v-show 区别 &amp;emsp;10">
<meta property="og:locale" content="en">
<meta property="og:image" content="https://raw.githubusercontent.com/ltadpoles/web-document/master/Other/images/mvc.png">
<meta property="og:image" content="https://raw.githubusercontent.com/ltadpoles/web-document/master/Other/images/mvvm.png">
<meta property="og:updated_time" content="2019-07-12T06:10:38.190Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="前端面试《第十五篇》 Vue.js">
<meta name="twitter:description" content="目录  &amp;emsp;1. 说一下Vue的双向绑定数据的原理 &amp;emsp;2. 解释单向数据流和双向数据绑定 &amp;emsp;3. Vue如何去除url中的 # &amp;emsp;4. 对 MVC、MVVM的理解 &amp;emsp;5. 介绍虚拟DOM &amp;emsp;6. vue生命周期的理解 &amp;emsp;7. 组件通信 &amp;emsp;8. 路由实现 &amp;emsp;9. v-if 和 v-show 区别 &amp;emsp;10">
<meta name="twitter:image" content="https://raw.githubusercontent.com/ltadpoles/web-document/master/Other/images/mvc.png">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: 'Author'
    }
  };
</script>




  <link rel="canonical" href="http://wurh.github.io/2019/03/31/201904010027/"/>


  <title> 前端面试《第十五篇》 Vue.js | 华Ing's blog </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="en">
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-PBHRK7"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

  










  
  
    
  

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-69412379-1', 'auto');
  ga('send', 'pageview');

</script>

  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b3ddfb8655d8b77429e04c7a4d5bcbc1";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();
  </script>


  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">华Ing's blog</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">harder and harder</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
            Tags
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                前端面试《第十五篇》 Vue.js
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">Posted on</span>
            <time itemprop="dateCreated" datetime="2019-03-31T16:31:12+08:00" content="2019-03-31">
              2019-03-31
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">In</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/前端面试/" itemprop="url" rel="index">
                    <span itemprop="name">前端面试</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/2019/03/31/201904010027/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="2019/03/31/201904010027/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2>目录</h2>

<p>&emsp;<a href="#k1">1. 说一下Vue的双向绑定数据的原理</a></p>
<p>&emsp;<a href="#k2">2. 解释单向数据流和双向数据绑定</a></p>
<p>&emsp;<a href="#k3">3. Vue如何去除url中的 #</a></p>
<p>&emsp;<a href="#k4">4. 对 MVC、MVVM的理解</a></p>
<p>&emsp;<a href="#k5">5. 介绍虚拟DOM</a></p>
<p>&emsp;<a href="#k6">6. vue生命周期的理解</a></p>
<p>&emsp;<a href="#k7">7. 组件通信</a></p>
<p>&emsp;<a href="#k8">8. 路由实现</a></p>
<p>&emsp;<a href="#k9">9. v-if 和 v-show 区别</a></p>
<p>&emsp;<a href="#k10">10. $route和$router的区别</a></p>
<p>&emsp;<a href="#k11">11. NextTick 是做什么的</a></p>
<p>&emsp;<a href="#k12">12. Vue 组件 data 为什么必须是函数</a></p>
<p>&emsp;<a href="#k13">13. 计算属性computed 和事件 methods 有什么区别</a></p>
<p>&emsp;<a href="#k14">14. 对比 jQuery ，Vue 有什么优缺点</a></p>
<p>&emsp;<a href="#k15">15. Vue 中怎么自定义指令</a></p>
<p>&emsp;<a href="#k16">16. Vue 中怎么自定义过滤器</a></p>
<p>&emsp;<a href="#k17">17. 对 keep-alive 的了解</a></p>
<p>&emsp;<a href="#k18">18. Vue 中 key 的作用</a></p>
<p>&emsp;<a href="#k19">19. 你觉得 Vue 的核心是什么</a></p>
<p>&emsp;<a href="#k20">20. vue 等单页面应用的优缺点</a></p>
<h5 id="k1">1. 说一下Vue的双向绑定数据的原理</h5>

<blockquote>
<p><code>vue</code> 实现数据双向绑定主要是：采用数据劫持结合发布者-订阅者模式的方式，通过 <code>Object.defineProperty()</code> 来劫持各个属性的 <code>setter</code>，<code>getter</code>，在数据变动时发布消息给订阅者，触发相应监听回调</p>
</blockquote>
<h5 id="k2">2. 解释单向数据流和双向数据绑定</h5>

<blockquote>
<p>单向数据流： 顾名思义，数据流是单向的。数据流动方向可以跟踪，流动单一，追查问题的时候可以更快捷。缺点就是写起来不太方便。要使UI发生变更就必须创建各种 <code>action</code> 来维护对应的 <code>state</code></p>
</blockquote>
<blockquote>
<p>双向数据绑定：数据之间是相通的，将数据变更的操作隐藏在框架内部。优点是在表单交互较多的场景下，会简化大量业务无关的代码。缺点就是无法追踪局部状态的变化，增加了出错时 <code>debug</code> 的难度</p>
</blockquote>
<h5 id="k3">3. Vue 如何去除url中的 #</h5>

<p>.<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> Router(&#123;</span><br><span class="line">  mode: <span class="string">'history'</span>,</span><br><span class="line">  routes: [ ]</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>需要注意的是，当我们启用 <code>history</code> 模式的时候，由于我们的项目是一个单页面应用，所以在路由跳转的时候，就会出现访问不到静态资源而出现 <code>404</code> 的情况，这时候就需要服务端增加一个覆盖所有情况的候选资源：如果 <code>URL</code> 匹配不到任何静态资源，则应该返回同一个 <code>index.html</code> 页面</p>
</blockquote>
<h5 id="k4">4. 对 MVC、MVVM的理解</h5>

<blockquote>
<p>MVC</p>
</blockquote>
<p><img src="https://raw.githubusercontent.com/ltadpoles/web-document/master/Other/images/mvc.png" alt="image"></p>
<p>特点：</p>
<ol>
<li><code>View</code> 传送指令到 <code>Controller</code></li>
<li><code>Controller</code> 完成业务逻辑后，要求 <code>Model</code> 改变状态</li>
<li><code>Model</code> 将新的数据发送到 <code>View</code>，用户得到反馈</li>
</ol>
<p><strong>所有通信都是单向的</strong></p>
<blockquote>
<p>MVVM</p>
</blockquote>
<p><img src="https://raw.githubusercontent.com/ltadpoles/web-document/master/Other/images/mvvm.png" alt="image"></p>
<p>特点： </p>
<ol>
<li>各部分之间的通信，都是双向的</li>
<li>采用双向绑定：<code>View</code> 的变动，自动反映在 <code>ViewModel</code>，反之亦然</li>
</ol>
<p>具体请移步 <a href="http://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html" target="_blank" rel="noopener">这里</a></p>
<h5 id="k5">5. 介绍虚拟DOM</h5>

<p><a href="https://www.jianshu.com/p/616999666920" target="_blank" rel="noopener">参考这里</a></p>
<h5 id="k6">6. vue生命周期的理解</h5>

<blockquote>
<p>vue实例有一个完整的生命周期，生命周期也就是指一个实例从开始创建到销毁的这个过程</p>
</blockquote>
<ul>
<li><code>beforeCreated()</code> 在实例创建之间执行，数据未加载状态</li>
<li><code>created()</code> 在实例创建、数据加载后，能初始化数据，<code>dom</code>渲染之前执行</li>
<li><code>beforeMount()</code> 虚拟<code>dom</code>已创建完成，在数据渲染前最后一次更改数据</li>
<li><code>mounted()</code> 页面、数据渲染完成，真实<code>dom</code>挂载完成</li>
<li><code>beforeUpadate()</code> 重新渲染之前触发</li>
<li><code>updated()</code> 数据已经更改完成，<code>dom</code> 也重新 <code>render</code> 完成,更改数据会陷入死循环</li>
<li><code>beforeDestory()</code> 和 <code>destoryed()</code> 前者是销毁前执行（实例仍然完全可用），后者则是销毁后执行</li>
</ul>
<h5 id="k7">7. 组件通信</h5>

<blockquote>
<p>父组件向子组件通信</p>
</blockquote>
<p>子组件通过 props 属性，绑定父组件数据，实现双方通信</p>
<blockquote>
<p>子组件向父组件通信</p>
</blockquote>
<p>将父组件的事件在子组件中通过 <code>$emit</code> 触发</p>
<blockquote>
<p>非父子组件、兄弟组件之间的数据传递</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*新建一个Vue实例作为中央事件总嫌*/</span></span><br><span class="line"><span class="keyword">let</span> event = <span class="keyword">new</span> Vue();</span><br><span class="line"></span><br><span class="line"><span class="comment">/*监听事件*/</span></span><br><span class="line">event.$on(<span class="string">'eventName'</span>, (val) =&gt; &#123;</span><br><span class="line">    <span class="comment">//......do something</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">/*触发事件*/</span></span><br><span class="line">event.$emit(<span class="string">'eventName'</span>, <span class="string">'this is a message.'</span>)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>Vuex 数据管理</p>
</blockquote>
<h5 id="k8">8. vue-router 路由实现</h5>

<blockquote>
<p>路由就是用来跟后端服务器进行交互的一种方式，通过不同的路径，来请求不同的资源，请求不同的页面是路由的其中一种功能</p>
</blockquote>
<p>参考 <a href="https://zhuanlan.zhihu.com/p/37730038" target="_blank" rel="noopener">这里</a></p>
<h5 id="k9">9. v-if 和 v-show 区别</h5>

<blockquote>
<p>使用了 <code>v-if</code> 的时候，如果值为 <code>false</code> ，那么页面将不会有这个 <code>html</code> 标签生成。</p>
</blockquote>
<blockquote>
<p><code>v-show</code> 则是不管值为 <code>true</code> 还是 <code>false</code> ，<code>html</code> 元素都会存在，只是 <code>CSS</code> 中的 <code>display</code> 显示或隐藏</p>
</blockquote>
<h5 id="k10">10. $route和$router的区别</h5>

<blockquote>
<p><code>$router</code> 为 <code>VueRouter</code> 实例，想要导航到不同 <code>URL</code>，则使用 <code>$router.push</code> 方法</p>
</blockquote>
<blockquote>
<p><code>$route</code> 为当前 <code>router</code> 跳转对象里面可以获取 <code>name</code> 、 <code>path</code> 、 <code>query</code> 、 <code>params</code> 等</p>
</blockquote>
<h5 id="k11">11. NextTick 是做什么的</h5>

<blockquote>
<p><code>$nextTick</code> 是在下次 <code>DOM</code> 更新循环结束之后执行延迟回调，在修改数据之后使用 <code>$nextTick</code>，则可以在回调中获取更新后的 <code>DOM</code></p>
</blockquote>
<p>具体可参考官方文档 <a href="https://cn.vuejs.org/v2/guide/reactivity.html" target="_blank" rel="noopener">深入响应式原理</a></p>
<h5 id="k12">12. Vue 组件 data 为什么必须是函数</h5>

<blockquote>
<p>因为js本身的特性带来的，如果 <code>data</code> 是一个对象，那么由于对象本身属于引用类型，当我们修改其中的一个属性时，会影响到所有Vue实例的数据。如果将 <code>data</code> 作为一个函数返回一个对象，那么每一个实例的 <code>data</code> 属性都是独立的，不会相互影响了</p>
</blockquote>
<h5 id="k13">13. 计算属性computed 和事件 methods 有什么区别</h5>

<p>我们可以将同一函数定义为一个 <code>method</code> 或者一个计算属性。对于最终的结果，两种方式是相同的</p>
<p>不同点：</p>
<blockquote>
<p>computed: 计算属性是基于它们的依赖进行缓存的,只有在它的相关依赖发生改变时才会重新求值</p>
</blockquote>
<blockquote>
<p>对于 <code>method</code> ，只要发生重新渲染，<code>method</code> 调用总会执行该函数</p>
</blockquote>
<h5 id="k14">14. 对比 jQuery ，Vue 有什么不同</h5>

<blockquote>
<p>jQuery 专注视图层，通过操作 DOM 去实现页面的一些逻辑渲染； Vue 专注于数据层，通过数据的双向绑定，最终表现在 DOM 层面，减少了 DOM 操作</p>
</blockquote>
<blockquote>
<p>Vue 使用了组件化思想，使得项目子集职责清晰，提高了开发效率，方便重复利用，便于协同开发</p>
</blockquote>
<h5 id="k15">15. Vue 中怎么自定义指令</h5>

<blockquote>
<p>全局注册</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 注册一个全局自定义指令 `v-focus`</span></span><br><span class="line">Vue.directive(<span class="string">'focus'</span>, &#123;</span><br><span class="line">  <span class="comment">// 当被绑定的元素插入到 DOM 中时……</span></span><br><span class="line">  inserted: <span class="function"><span class="keyword">function</span> (<span class="params">el</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 聚焦元素</span></span><br><span class="line">    el.focus()</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>局部注册</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">directives: &#123;</span><br><span class="line">  focus: &#123;</span><br><span class="line">    <span class="comment">// 指令的定义</span></span><br><span class="line">    inserted: <span class="function"><span class="keyword">function</span> (<span class="params">el</span>) </span>&#123;</span><br><span class="line">      el.focus()</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>参考 <a href="https://cn.vuejs.org/v2/guide/custom-directive.html" target="_blank" rel="noopener">官方文档-自定义指令</a></p>
<h5 id="k16">16. Vue 中怎么自定义过滤器</h5>

<blockquote>
<p>可以用全局方法 <code>Vue.filter()</code> 注册一个自定义过滤器，它接收两个参数：过滤器 <code>ID</code> 和过滤器函数。过滤器函数以值为参数，返回转换后的值</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Vue.filter(<span class="string">'reverse'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">value</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> value.split(<span class="string">''</span>).reverse().join(<span class="string">''</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 'abc' =&gt; 'cba' --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">span</span> <span class="attr">v-text</span>=<span class="string">"message | reverse"</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>过滤器也同样接受全局注册和局部注册</p>
<h5 id="k17">17. 对 keep-alive 的了解</h5>

<blockquote>
<p><code>keep-alive</code> 是 <code>Vue</code> 内置的一个组件，可以使被包含的组件保留状态，或避免重新渲染</p>
</blockquote>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">keep-alive</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">component</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 该组件将被缓存！ --&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">component</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">keep-alive</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>可以使用API提供的props，实现组件的动态缓存</p>
</blockquote>
<p>具体参考 <a href="https://cn.vuejs.org/v2/api/#keep-alive" target="_blank" rel="noopener">官方API</a></p>
<h5 id="k18">18. Vue 中 key 的作用</h5>

<blockquote>
<p><code>key</code> 的特殊属性主要用在 <code>Vue</code> 的虚拟 <code>DOM</code> 算法，在新旧 <code>nodes</code> 对比时辨识 <code>VNodes</code>。如果不使用 <code>key</code>，<code>Vue</code> 会使用一种最大限度减少动态元素并且尽可能的尝试修复/再利用相同类型元素的算法。使用 <code>key</code>，它会基于 <code>key</code> 的变化重新排列元素顺序，并且会移除 <code>key</code> 不存在的元素。</p>
</blockquote>
<blockquote>
<p>有相同父元素的子元素必须有独特的 <code>key</code>。重复的 <code>key</code> 会造成渲染错误</p>
</blockquote>
<p>具体参考 <a href="https://cn.vuejs.org/v2/api/#key" target="_blank" rel="noopener">官方API</a></p>
<h5 id="k19">19. Vue 的核心是什么</h5>

<blockquote>
<p>数据驱动 组件系统</p>
</blockquote>
<h5 id="k20">20. vue 等单页面应用的优缺点</h5>

<blockquote>
<p>优点：</p>
</blockquote>
<ul>
<li>良好的交互体验 </li>
<li>良好的前后端工作分离模式</li>
<li>减轻服务器压力</li>
</ul>
<blockquote>
<p>缺点：</p>
</blockquote>
<ul>
<li>SEO难度较高</li>
<li>前进、后退管理 </li>
<li>初次加载耗时多 </li>
</ul>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        
  <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
    <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
    <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
      <span>赏</span>
    </button>
    <div id="QR" style="display: none;">
      
        <div id="wechat" style="display: inline-block">
          <img id="wechat_qr" src="https://wurh.github.io/images/others/weixinshou.png" alt="华Ing WeChat Pay"/>
          <p>微信打赏</p>
        </div>
      
      
        <div id="alipay" style="display: inline-block">
          <img id="alipay_qr" src="https://wurh.github.io/images/others/alifu.png" alt="华Ing Alipay"/>
          <p>支付宝打赏</p>
        </div>
      
    </div>
  </div>


      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/前端-知识-面试/" rel="tag">#前端 知识 面试</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/03/31/201904010024/" rel="next" title="前端面试《第十四篇》 webpack">
                <i class="fa fa-chevron-left"></i> 前端面试《第十四篇》 webpack
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/03/31/201904010033/" rel="prev" title="前端面试《第十六篇》 html+css">
                前端面试《第十六篇》 html+css <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        
<script>
  with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
</script>

      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="2019/03/31/201904010027/"
           data-title="前端面试《第十五篇》 Vue.js" data-url="http://wurh.github.io/2019/03/31/201904010027/">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            Overview
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="https://wurh.github.io/images/avatar/logo.jpg?123456"
               alt="华Ing" />
          <p class="site-author-name" itemprop="name">华Ing</p>
          <p class="site-description motion-element" itemprop="description">harder and harder</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">56</span>
              <span class="site-state-item-name">posts</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">categories</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">15</span>
                <span class="site-state-item-name">tags</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/wurh" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/1674582564/profile?topnav=1&wvr=6" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/wu-rong-hua-56" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://fsiaonma.github.io/" title="sam博客" target="_blank">sam博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://shenjoel.github.io/" title="joel博客" target="_blank">joel博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://firewood1122.github.io" title="firewood博客" target="_blank">firewood博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://reqianduan.com" title="热前端" target="_blank">热前端</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://opentutorial.info/" title="黄超博客" target="_blank">黄超博客</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#undefined"><span class="nav-number">1.</span> <span class="nav-text">目录</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#k1"><span class="nav-number">1.0.0.1.</span> <span class="nav-text">1. 说一下Vue的双向绑定数据的原理</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k2"><span class="nav-number">1.0.0.2.</span> <span class="nav-text">2. 解释单向数据流和双向数据绑定</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k3"><span class="nav-number">1.0.0.3.</span> <span class="nav-text">3. Vue 如何去除url中的 #</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k4"><span class="nav-number">1.0.0.4.</span> <span class="nav-text">4. 对 MVC、MVVM的理解</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k5"><span class="nav-number">1.0.0.5.</span> <span class="nav-text">5. 介绍虚拟DOM</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k6"><span class="nav-number">1.0.0.6.</span> <span class="nav-text">6. vue生命周期的理解</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k7"><span class="nav-number">1.0.0.7.</span> <span class="nav-text">7. 组件通信</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k8"><span class="nav-number">1.0.0.8.</span> <span class="nav-text">8. vue-router 路由实现</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k9"><span class="nav-number">1.0.0.9.</span> <span class="nav-text">9. v-if 和 v-show 区别</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k10"><span class="nav-number">1.0.0.10.</span> <span class="nav-text">10. $route和$router的区别</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k11"><span class="nav-number">1.0.0.11.</span> <span class="nav-text">11. NextTick 是做什么的</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k12"><span class="nav-number">1.0.0.12.</span> <span class="nav-text">12. Vue 组件 data 为什么必须是函数</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k13"><span class="nav-number">1.0.0.13.</span> <span class="nav-text">13. 计算属性computed 和事件 methods 有什么区别</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k14"><span class="nav-number">1.0.0.14.</span> <span class="nav-text">14. 对比 jQuery ，Vue 有什么不同</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k15"><span class="nav-number">1.0.0.15.</span> <span class="nav-text">15. Vue 中怎么自定义指令</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k16"><span class="nav-number">1.0.0.16.</span> <span class="nav-text">16. Vue 中怎么自定义过滤器</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k17"><span class="nav-number">1.0.0.17.</span> <span class="nav-text">17. 对 keep-alive 的了解</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k18"><span class="nav-number">1.0.0.18.</span> <span class="nav-text">18. Vue 中 key 的作用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k19"><span class="nav-number">1.0.0.19.</span> <span class="nav-text">19. Vue 的核心是什么</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#k20"><span class="nav-number">1.0.0.20.</span> <span class="nav-text">20. vue 等单页面应用的优缺点</span></a></li></ol></li></ol></li></ol></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2013 - 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">华Ing</span>
</div>

<div class="powered-by">
  Powered by <a class="theme-link" href="https://hexo.io">Hexo</a>
</div>

<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.0.2"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"wurh"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  

  

  

  

  


</body>
</html>
